package com.traffic.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.traffic.annotation.Power;
import com.traffic.constant.Const;
import com.traffic.constant.Status;
import com.traffic.entity.Issue;
import com.traffic.exception.BaseException;
import com.traffic.mapper.IssueMapper;
import com.traffic.service.IssueService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
//@CacheConfig(cacheNames = "info")

public class IssueServiceImpl  extends ServiceImpl<IssueMapper, Issue> implements IssueService{

    @Autowired IssueMapper issueMapper;

    @Power(authority = Const.AUTH_ADD)
    @Override
    public Issue saveIssue(Issue issue) {

        issueMapper.insert(issue);

        return issue;
    }

    @Override
    @Power(authority = Const.AUTH_EDIT)
    public Issue updateIssue(Issue issue) {

        issueMapper.updateById(issue);
        return issue;
    }

    @Override
    public List<Issue> queryTargetIssue(String ques,String startDate,String endDate) {

        QueryWrapper<Issue> wrapper = new QueryWrapper<>();
      //  wrapper.like("question",ques).between("create_time",startDate,endDate);
        wrapper.like("question",ques).and(StringUtils.isNotBlank(startDate)
                        && StringUtils.isNotBlank(endDate),
                dateSql -> dateSql.ge("create_time", startDate.toString())
                        .apply("create_time <= DATE_ADD({0}, INTERVAL 1 DAY)", endDate.toString()));
        List<Issue> questions = issueMapper.selectList(wrapper);

        return questions;
    }

    @Override
    @Power(authority = Const.AUTH_DELETE)
    public boolean deleteIssue(Long id) {

        issueMapper.deleteById(id);

        return true;
    }

}
